package br.com.locanet.view.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.locanet.bean.impl.Filme;
import br.com.locanet.service.impl.FilmeService;

public class ImageViewServlet extends HttpServlet {
	private static final long serialVersionUID = -5181432832726074158L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		if (req.getParameter("img")!=null){
			Long id = Long.parseLong(req.getParameter("img"));
			
			FilmeService filmeService = new FilmeService();
			try {
				Filme filme = filmeService.selectById(new Filme(id));
				if (filme!=null){
			        resp.setContentType("image/jpeg");
			        resp.setHeader("Content-disposition", "attachment;filename=imagem"+id+"");
			        resp.getOutputStream().write(filme.getImagemFilme());
			        resp.getCharacterEncoding();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
	}
}
